﻿Imports Declarations.Events

Public Class ucControls
    Public _Player As OCDSCore
    Public _OpenFileDialog As OpenFileDialog
    Public _VideoWindow As ucWindow
    Public _FullscreenWindow As frmFullscreen
    Public _mainpanel As Panel
    Private _mouseactive As Boolean
    Private _ExpandedClientRectangle As System.Drawing.Rectangle
    Private _toggleFullscreen As Boolean = False

    Public Property ToggleFullscreen As Boolean
        Get
            Return _toggleFullscreen
        End Get
        Set(value As Boolean)
            _toggleFullscreen = value
        End Set
    End Property

    Public Property Mouseactive As Boolean
        Get
            Return _mouseactive
        End Get
        Set(value As Boolean)
            _mouseactive = value
        End Set
    End Property

    Public Sub Init(ocdsinstance As OCDSCore, openfileinstance As OpenFileDialog, windowinstance As ucWindow, mainpanelinstance As Panel, fullscreeninstance As frmFullscreen)
        _Player = ocdsinstance
        _OpenFileDialog = openfileinstance
        _VideoWindow = windowinstance
        _mainpanel = mainpanelinstance
        _FullscreenWindow = fullscreeninstance
    End Sub
    Private Sub btnOpen_Click(sender As Object, e As EventArgs) Handles btnOpen.Click
        LoadMedia()
        _Player.Play()
    End Sub

    Private Sub LoadMedia()
        If _OpenFileDialog.ShowDialog() = DialogResult.OK Then
            _Player.MediaPath = _OpenFileDialog.FileName
        End If
    End Sub

    Private Sub btnPlay_Click(sender As Object, e As EventArgs) Handles btnPlay.Click
        If Not String.IsNullOrEmpty(_Player.MediaPath) Then

            If _Player.CanPause = True Then
                btnPlay.Text = "Pause"
                btnPlay.Image = My.Resources.resPauseX

            ElseIf _Player.CanPause = False Then
                btnPlay.Text = "Play"
                btnPlay.Image = My.Resources.resStartX
                _Player.Play()
            End If
        Else
            MessageBox.Show("Please select media path first !")
        End If
    End Sub

    Private Sub tbVolume_Scroll(sender As System.Object, e As System.Windows.Forms.ScrollEventArgs) Handles tbVolume.Scroll
        _Player.Volume = tbVolume.Value
        If _Player.Mute = False Then
            If tbVolume.Value >= 50 Then
                btnMute.Image = My.Resources.Resources.resSound_full
            ElseIf tbVolume.Value <= 1 Then
                btnMute.Image = My.Resources.Resources.resSound_none
            ElseIf tbVolume.Value <= 49 Then
                btnMute.Image = My.Resources.Resources.resSound_low
            End If
        End If
    End Sub

    Private Sub btnStop_Click(sender As Object, e As EventArgs) Handles btnStop.Click
        _Player.[Stop]()
    End Sub

    Private Sub btnPause_Click(sender As Object, e As EventArgs)
        _Player.Pause()
    End Sub

    Private Sub btnMute_Click(sender As Object, e As EventArgs) Handles btnMute.Click
        _Player.ToggleMute()

        If _Player.Mute = False Then
            If tbVolume.Value >= 50 Then
                btnMute.Image = My.Resources.Resources.resSound_full
            ElseIf tbVolume.Value <= 1 Then
                btnMute.Image = My.Resources.Resources.resSound_none
            ElseIf tbVolume.Value <= 49 Then
                btnMute.Image = My.Resources.Resources.resSound_low
            End If
        ElseIf _Player.Mute = True Then
            btnMute.Image = My.Resources.Resources.resSound_mute
            btnMute.ImageAlign = ContentAlignment.MiddleLeft
        End If

    End Sub

    Private Sub tbProcess_Scroll(sender As System.Object, e As System.Windows.Forms.ScrollEventArgs) Handles tbProcess.Scroll
        _Player.Position = CSng(tbProcess.Value) / 100
    End Sub

    Private Sub btnFullscreen_Click(sender As System.Object, e As System.EventArgs) Handles btnFullscreen.Click

        If ToggleFullscreen = True Then
            _FullscreenWindow.ExitFullscreen()
        ElseIf ToggleFullscreen = False Then
            _FullscreenWindow.Init(Me, _VideoWindow)
        End If

    End Sub

    Private Sub frmControls_MouseEnter(ByVal sender As Object, ByVal e As System.EventArgs) Handles MyBase.MouseEnter
        Const Margin As Integer = 5

        'Set properties to indicate the mouse is over the control 'me'
        Mouseactive = True
        'Calculate an expanded ClientRectangle
        _ExpandedClientRectangle = ExpandedClientScreenRectangle(Me, Margin)
        'Initialise the timer
        Timer1.Interval = 100
        Timer1.Start()
    End Sub

    Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As System.EventArgs) Handles Timer1.Tick
        If _ExpandedClientRectangle.Contains(Cursor.Position) = False Then
            'The cursor has definitely left the control

            'Set properties to indicate the mouse has left the control 'me'
            Mouseactive = False

            'Turn the timer off
            Timer1.Stop()
        End If
    End Sub

    Private Function ExpandedClientScreenRectangle(ByVal Ctrl As Control, ByVal Margin As Integer) As Rectangle
        'Returns a rectangle that is expanded by the value Margin.

        Dim CtrlRect As Rectangle = RectangleToScreen(Ctrl.ClientRectangle)

        ExpandedClientScreenRectangle = New Rectangle

        With ExpandedClientScreenRectangle
            .X = CtrlRect.X - Margin
            .Y = CtrlRect.Y - Margin
            .Width = CtrlRect.Width + (2 * Margin)
            .Height = CtrlRect.Height + (2 * Margin)
        End With
    End Function

    Public Sub Events_TimeChanged(sender As Object, e As MediaPlayerTimeChanged)
        lblTime.Text = TimeSpan.FromMilliseconds(e.NewTime).ToString().Substring(0, 8)
    End Sub

    Public Sub Events_PlayerPositionChanged(sender As Object, e As MediaPlayerPositionChanged)
        tbProcess.Value = CInt(e.NewPosition * 100)
    End Sub

    Public Sub Events_StateChanged(sender As Object, e As MediaStateChange)
        lblPlayerState.Text = e.NewState.ToString()
    End Sub

    Public Sub Events_DurationChanged(sender As Object, e As MediaDurationChange)
        lblDuration.Text = TimeSpan.FromMilliseconds(e.NewDuration).ToString().Substring(0, 8)
    End Sub

    Public Sub InitControls()
        tbProcess.Value = 0
        lblTime.Text = "00:00:00"
        lblDuration.Text = "00:00:00"
    End Sub
End Class
